package com.github.wxiaoqi.security.biz.biz;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.entity.BhResumUsed;
import com.github.wxiaoqi.security.biz.entity.BhResume;
import com.github.wxiaoqi.security.biz.entity.BhTask;
import com.github.wxiaoqi.security.biz.entity.BhTaskUserResume;
import com.github.wxiaoqi.security.biz.mapper.BhResumUsedMapper;
import com.github.wxiaoqi.security.biz.mapper.BhResumeMapper;
import com.github.wxiaoqi.security.biz.mapper.BhTaskMapper;
import com.github.wxiaoqi.security.biz.mapper.BhTaskUserResumeMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.util.EntityUtils;
import com.netflix.infix.lang.infix.antlr.EventFilterParser.regex_predicate_return;

import lombok.extern.slf4j.Slf4j;

/**
 * 任务-猎手-简历关系表     存储用户发送的简历的信息
 *
 * @author Mr.AG
 * @email 463540703@qq.com
 * @date 2018-05-16 10:29:08
 */
@Slf4j
@Service
public class BhTaskUserResumeBiz extends BaseBiz<BhTaskUserResumeMapper,BhTaskUserResume> {

	@Autowired
	private BhResumUsedMapper bhResumUsedMapper;
	@Autowired
	private BhResumeMapper bhResumeMapper;
	@Autowired
	private BhTaskMapper bhTaskMapper;
	
	public Map<String, Object> sendResume(Map<String, Object> req) throws Exception {
		log.info("投简历biz传入参数{}", req);
		Map<String, Object> result = new HashMap<String, Object>();
		BhTaskUserResume bhTaskUserResume = EntityUtils.map2Bean(req, BhTaskUserResume.class);
		bhTaskUserResume.setCreatetime(new Date());
		bhTaskUserResume.setId(String.valueOf(SnowFlake.getId()));
		BhTask task = bhTaskMapper.selectByPrimaryKey(bhTaskUserResume.getTaskId());
		//任务不存在
		if(task == null){
			result.put("code", ResponseCode.TASK_NOTEXIST.getCode());
			result.put("msg", ResponseCode.TASK_NOTEXIST.getMessage());
			result.put("sucess", false);
			log.info("投简历biz结束{}", result);
			return result;
		}
		//简历不存在
		BhResume resume = bhResumeMapper.selectByPrimaryKey(bhTaskUserResume.getResumeId());
		if(resume == null){
			result.put("code", ResponseCode.RESUM_NOTEXIST.getCode());
			result.put("msg", ResponseCode.RESUM_NOTEXIST.getMessage());
			result.put("sucess", false);
			log.info("投简历biz结束{}", result);
			return result;
		}
		//同一份简历多次投递
		BhTaskUserResume btur = new BhTaskUserResume();
		btur.setTaskId(bhTaskUserResume.getTaskId());
		btur.setResumeId(resume.getId());
		List<BhTaskUserResume> list = mapper.select(btur);
		if(list.size() > 0){
			result.put("code", ResponseCode.RESUM_REPEAT_SEND.getCode());
			result.put("msg", ResponseCode.RESUM_REPEAT_SEND.getMessage());
			result.put("sucess", false);
			log.info("投简历biz结束{}", result);
			return result;
		}
		//电话相同简历不能重复
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mobile", resume.getMobile());
		map.put("taskId", bhTaskUserResume.getTaskId());
		int count = mapper.querySameMobileResume(map);
		if(count > 0){
			result.put("code", ResponseCode.RESUM_REPEAT_SEND_USER.getCode());
			result.put("msg", ResponseCode.RESUM_REPEAT_SEND_USER.getMessage());
			result.put("sucess", false);
			log.info("投简历biz结束{}", result);
			return result;
		}
		BhResumUsed bhResumUsed = new BhResumUsed();
		BeanUtils.copyProperties(resume,bhResumUsed);
		bhResumUsed.setId(String.valueOf(SnowFlake.getId()));
		bhResumUsed.setResumeId(task.getId());
		bhResumUsed.setStatus("0");
		bhResumUsed.setMerId(task.getMerId());
		bhResumUsed.setCreateTime(new Timestamp(new Date().getTime()));
		bhResumUsed.setUpdateTime(new Timestamp(new Date().getTime()));
		
		
		
		bhResumUsedMapper.insert(bhResumUsed);
		bhTaskUserResume.setResumeId(bhResumUsed.getId());
		mapper.insert(bhTaskUserResume);
		result.put("code", ResponseCode.OK.getCode());
		result.put("sucess", true);
		result.put("msg", ResponseCode.OK.getMessage());
		log.info("投简历biz结束{}", result);
		return result;
	}
}